<?php

namespace app\common\model;

use app\common\model\Base;
use app\common\model\User;
use think\Model;

class OrderList extends Base
{

    // 设置当前模型对应的完整数据表名称
    protected $table = 'order_list';

    /**
     * 获取列表
     */
    public static function getList($where = [], $field = '*', $order = '', $pageSize = "15")
    {
        $list = self::where($where)
            ->field($field)
            ->order($order)
            ->paginate(['list_rows' => $pageSize, 'query' => request()->param()]);
        $page = $list->render();
        $data['list'] = $list->toArray()['data'];
        $data['count'] = $list->total();
        $data['last_page'] = $list->toArray()['last_page'];
        $data['page'] = $page;
        return $data;
    }

    /**
     * 获取列表  不分页
     */
    public static function getAllList($where = [], $field = '*', $order = '', $limit = '0')
    {
        $data = self::where($where)
            ->field($field)
            ->order($order)
            ->limit($limit)
            ->select();
        return $data;
    }

    /**
     * 获取单条数据
     */
    public static function getInfo($where = [], $field = '*')
    {
        $data = self::where($where)
            ->field($field)
            ->find();
        return $data;
    }

    public function getShangTitleAttr($value, $data)
    {
        if (isset($data['shang_id'])) {
            $title = Shang::field('title')->where(['id' => $data['shang_id']])->value('title')??'无';
            return $title;
        } else {
            return '';
        }
    }

    public function getGoodsTitleAttr($value, $data)
    {
        if (isset($data['goods_id'])) {
            $title = Goods::field('title')->where(['id' => $data['goods_id']])->value('title');
            return $title;
        } else {
            return '';
        }
    }

    public function getSpecialImgurlAttr($value, $data)
    {
        if (isset($data['shang_id'])) {
            $special_imgurl = Shang::field('special_imgurl')->where(['id' => $data['shang_id']])->value('special_imgurl');
            return imageUrl($special_imgurl);
        } else {
            return '';
        }
    }

    public function getUserInfoAttr($value, $data)
    {
        $user_info = User::field('nickname,headimg')->where(['id' => $data['user_id']])->find();
        return $user_info;
    }

    public function getCardNoAttr($value, $data)
    {
        if (isset($data['goodslist_id'])) {
            $card_no = GoodsList::field('card_no')->where(['id' => $data['goodslist_id']])->value('card_no');
            return $card_no;
        } else {
            return '';
        }
    }


}